package com.it.test;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Properties;

public class Server {
    static ArrayList<Socket> list = new ArrayList<>();
    public static void main(String[] args) throws IOException {
        ServerSocket serverSocket = new ServerSocket(1000);
        //获取文件中正确的用户名及密码
        Properties prop = new Properties();
        FileInputStream fis = new FileInputStream("D:\\java\\basic-code\\online-chat-room\\客户数据.txt");
        prop.load(fis);
        //只要有一个客户端，就开一条线程处理
        while (true) {
            Socket socket = serverSocket.accept();
            System.out.println("有客户端来连接");
            new Thread(new MyRunnable(socket,prop)).start();
        }
    }
}
class MyRunnable implements Runnable {
    Socket socket;
    Properties prop;
    public MyRunnable(Socket socket, Properties prop) {
        this.socket = socket;
        this.prop = prop;
    }
    public void run() {
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            while (true) {
                String choose = br.readLine();
                switch (choose) {
                    case "login" -> login(br);
                    case "register" -> System.out.println("用户选择了注册操作");
                }
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    //该方法要获取输入的用户名和密码
    //并进行判断
    public void login(BufferedReader br) throws IOException {
        System.out.println("用户选择了登录操作");
        String userinfo = br.readLine();
        //username=zhangsan&password=123
        //对搜到的用户信息进行拆分
        String[] userinfoArr = userinfo.split("&");
        String usernameInput = userinfoArr[0].split("=")[1];
        String passwordInput = userinfoArr[1].split("=")[1];
        System.out.println("用户名为："+usernameInput);
        System.out.println("密码为："+passwordInput);

        //判断
        if(prop.containsKey(usernameInput)){
            //用户名存在，则继续判断密码是否正确
            String rightpassword = prop.get(usernameInput)+"";
            //判断密码是否正确
            if(rightpassword.equals(passwordInput)){
                //提示用户登录成功
                writeMessage2Client("1");
                //有客户端来连接，就把它的socket保存起来
                Server.list.add(socket);
                //接收客户端发来的信息
                talk2All(br,usernameInput);
            }else{
                //密码错误，直接回写
                writeMessage2Client("2");
            }
        }else{
            //用户名不存在，直接回写
            writeMessage2Client("3");
        }
    }

    private void talk2All(BufferedReader br, String usernameInput) throws IOException {
        while (true) {
            String message = br.readLine();
            System.out.println(usernameInput+"发送过来消息："+message);
            //群发消息
            for(Socket s : Server.list){
                writeMessage2Client(s,usernameInput+"发送过来消息："+message);
            }
        }
    }

    public void writeMessage2Client(String message) throws IOException {
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
        bw.write(message);
        bw.newLine();
        bw.flush();
    }
    public void writeMessage2Client(Socket s, String message) throws IOException {
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
        bw.write(message);
        bw.newLine();
        bw.flush();
    }
}
